//
//  PopAddContentView.m
//  HuaRun2
//
//  Created by admin on 17/9/7.
//  Copyright © 2017年 Sunny. All rights reserved.
//

#import "PopAddContentView.h"
#import "LineView.h"

@interface PopAddContentView ()
    
//取消按钮
@property (nonatomic, strong)UIButton * cancelBtn;

//公司
@property (nonatomic, strong)UIView * companyView;
//联系人
@property (nonatomic, strong)UIView * contactView;
//扫描
@property (nonatomic, strong)UIView * scanView;

//背景图
@property (nonatomic, strong)UIImageView * bgImageView;
    //说明文字图片
@property (nonatomic, strong)UIImageView * infoImageView;

//线1
@property (nonatomic, strong) LineView * line1;
    

//是否选择了公司
@property(nonatomic, assign)BOOL isSelectCompany;
//是否扫描
@property(nonatomic, assign)BOOL isSelectScan;

@end

@implementation PopAddContentView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark - life cycle
- (void)dealloc {
NSLog(@"%@ dealloc", [self class]);
}

- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
    if (self) {
        [self viewInit];
        [self layoutInit];
    }
    return self;
}

- (void)viewInit {
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissSelfMethod)];
    [self addGestureRecognizer:tap];
    
    [self addSubview:self.bgImageView];
    [self addSubview:self.infoImageView];
    [self addSubview:self.cancelBtn];
    [self addSubview:self.line1];

    
    self.companyView = [self makeCommonViewWithText:NSLocalizedString(@"公司", nil)
                                          imageName:@"公司-拷贝-3"
                                       selectedName:@"公司001"];
    [self addSubview:self.companyView];

    self.contactView = [self makeCommonViewWithText:NSLocalizedString(@"联系人",nil)
                                          imageName:@"联系人-拷贝-3"
                                       selectedName:@"icon_lianxiren"];
    [self addSubview:self.contactView];

    self.scanView = [self makeCommonViewWithText:NSLocalizedString(@"scan_add_contact",nil)
                                       imageName:@"名片扫描"
                                    selectedName:@"icon_lianxiren"];
    [self addSubview:self.scanView];

    weakself(ws);
    

    [self.companyView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws selMethod:gestureRecoginzer.view];
    }];
    
    [self.contactView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws selMethod:gestureRecoginzer.view];
    }];
    
    [self.scanView addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        [ws selMethod:gestureRecoginzer.view];
    }];
    
}
    
- (void)selMethod:(UIView *)view {
    
    ClickType type = -1;
    if (view == _companyView) {
        type = 0;
    }else if (view == _contactView) {
        type = 1;
    }else if (view == _scanView) {
        type = 2;
    }
    
    [self dismissSelfMethod];
    _clickTypeBlock ? _clickTypeBlock(type) : nil;

}
    
- (void)layoutInit {
    
    weakself(ws);
    [self.infoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws);
        make.top.mas_equalTo(ws).offset(150);
    }];

    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws);
        make.bottom.mas_equalTo(ws).offset(-30);
        make.size.mas_equalTo(CGSizeMake(15, 15));
    }];
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(ws);
        make.bottom.mas_equalTo(ws).offset(-70);
        make.size.mas_equalTo(CGSizeMake(kMainScreenWidth, 1));
    }];


    [self.companyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.line1.mas_top).offset(-kMargin10 * 4);
        make.size.mas_equalTo(CGSizeMake(80, 80));
        make.right.mas_equalTo(ws.contactView.mas_left).offset(-kMargin10 * 3);
    }];

    [self.contactView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.companyView);
        make.size.mas_equalTo(ws.companyView);
        make.centerX.mas_equalTo(ws.mas_centerX);
    }];

    [self.scanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(ws.companyView);
        make.size.mas_equalTo(ws.companyView);
        make.left.mas_equalTo(ws.contactView.mas_right).offset(kMargin10 * 3);
    }];

}



- (void)dismissSelfMethod {
    
    weakself(ws);
    
    [UIView animateWithDuration:0.3 animations:^{
        
    } completion:^(BOOL finished) {
        
        [ws removeFromSuperview];
    }];
    
}

#pragma mark getter setter
- (UIImageView *)bgImageView {
    if (!_bgImageView) {
        self.bgImageView = [UIImageView new];
        _bgImageView.image = [UIImage imageNamed:@"矩形"];
        _bgImageView.frame = self.frame;
    }
    return _bgImageView;
}

- (UIImageView *)infoImageView {
    if (!_infoImageView) {
        self.infoImageView = [UIImageView new];
        _infoImageView.image = [UIImage imageNamed:
                               NSLocalizedString(@"brightest-opportunity", nil)];
    }
    return _infoImageView;
}

    
- (UIButton *)cancelBtn{
    if (!_cancelBtn) {
        self.cancelBtn = [UIButton new];
        [_cancelBtn setBackgroundImage:[UIImage imageNamed:@"关闭按钮"]
                              forState:(UIControlStateNormal)];
        [_cancelBtn addTarget:self action:@selector(dismissSelfMethod) forControlEvents:(UIControlEventTouchUpInside)];
    }
    return _cancelBtn;
}
    
- (LineView *)line1 {
    if (!_line1) {
        self.line1 = [LineView new];
    }
    return _line1;
}


- (UIView *)makeCommonViewWithText:(NSString *)text imageName:(NSString *)imageName
                      selectedName:(NSString *)selectedName{
    UIView * view = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 80, 80))];
    view.backgroundColor = [UIColor clearColor];
    
    UIButton * btn = [UIButton new];
    [btn setBackgroundImage:[UIImage imageNamed:imageName]
                   forState:(UIControlStateNormal)];
    [btn setBackgroundImage:[UIImage imageNamed:selectedName]
                   forState:(UIControlStateSelected)];

    [btn setTitleColor:[UIColor colorWithHex:@"c3c3c3"]
                     forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor whiteColor]
                     forState:(UIControlStateSelected)];
    btn.userInteractionEnabled = NO;
    btn.tag = 100;
    [view addSubview:btn];
    
    UILabel * label = [[UILabel alloc] init];
    label.font = kFontSize15;
    label.text = text;
    label.tag = 200;
    label.textColor = [UIColor grayColor];
    [view addSubview:label];
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view);
        make.top.mas_equalTo(view);
        make.size.mas_equalTo(CGSizeMake(50, 50));
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(view);
        make.top.mas_equalTo(btn.mas_bottom).offset(kMargin10);
        make.height.mas_equalTo(20);
    }];
    
    return view;
}

@end
